<?php

namespace app\health\model;

use think\Model;


class HealthArticle extends Model
{
    // 表名
    protected $name = 'health_article';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'author',
        'total_views'
    ];

    public function category()
    {
        return $this->belongsTo(HealthArticleCategory::class, 'category_id', 'id');
    }

    public function getAuthorAttr($value, $data)
    {
        return '平安健康';
    }

    /**
     * 获取文章总浏览量 真实+虚拟
     * @Author 张旭鹏
     * @DateTime 2023-06-14
     */
    public function getTotalViewsAttr($value, $data)
    {
        $actualViews = isset($data['actual_views']) ? $data['actual_views'] : 0;
        $virtualViews = isset($data['virtual_views']) ? $data['virtual_views'] : 0;
        return $actualViews + $virtualViews;
    }
}
